package com.define.ssh.dao.impl;


import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import org.hibernate.CacheMode;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.springframework.stereotype.Repository;

import com.define.ssh.dao.GroupmemberRegularDao;
import com.define.ssh.model.GroupmemberRegular;

@Repository("groupmemberRegularDaoImpl")
public class GroupmemberRegularDaoImpl extends BaseDaoImpl<GroupmemberRegular, Long> implements GroupmemberRegularDao {
	
	
	public Integer gmcount(Integer pid) {
		Integer i=0;
		System.out.println(pid);
		String sql = "select count(*) from compdept where pid=" + pid;
		Object  cdCount=getSession().createSQLQuery(sql).uniqueResult();
		
		String sql1 = "select count(*) from deptuser where deptid=" + pid;
		Object  deCount=getSession().createSQLQuery(sql1).uniqueResult();
		
		i=i+Integer.valueOf(cdCount.toString());
		i=i+Integer.valueOf(deCount.toString());
		return i;
	}
	
	public void updateAllUpdatetype(String groupid,Integer updatetype){
		String sql = "update groupmember_regular t  set t.updatetype="+updatetype+", update_Time=NOW()  where 1=1 ";
		sql += " and t.groupid='" + groupid + "'";
		getSession().createSQLQuery(sql).executeUpdate();
	}
	
	@Override
	public void save(List<GroupmemberRegular> list) {
		
		if (list != null && list.size() > 0)
		{
			SimpleDateFormat sdf=new SimpleDateFormat("HH:mm:ss");
			String start=sdf.format(new Date());
			
			//批量导入start
		    Session session = this.sessionFactory.getCurrentSession();//  HibernateUtil.getSessionFactory().getCurrentSession();   
		    Transaction tran = session.beginTransaction(); 
	           session.setCacheMode(CacheMode.IGNORE);   
	           PreparedStatement stmt;   
	           try {
	               stmt = session.connection().prepareStatement("insert into groupmember_regular(groupid,userid,update_time,updatetype,pushflag,isadmin) " +
	               		"values(?,?,?,?,?,?)");   
	               for(GroupmemberRegular da:list){
	            	   stmt.setString(1, da.getGroupid());
	            	   stmt.setInt(2, da.getUserid());
	            	   stmt.setDate(3, new java.sql.Date(System.currentTimeMillis())); 
	            	   stmt.setInt(4,da.getUpdatetype());
	            	   stmt.setInt(5, da.getPushflag());
	            	   stmt.setInt(6, da.getIsadmin());
	            	   stmt.addBatch();
	               }
	               stmt.executeBatch();
	        	  
	           } catch (SQLException e) {   
	               e.printStackTrace();    
	               tran.rollback();   
	           } finally {
	        	   tran.commit(); 
	        	    session.close();   
	        
	        	  }  
	            
	           
	          // session.close();
	           session.clear();
	           //this.trcvCustService.getDao().getSessionFactory().close();   
			String end=sdf.format(new Date());
			System.out.println("导入完成，导入条数："+list.size());
			System.out.println("开始时间："+start);
			System.out.println("结束时间："+end);
		}
	}
}
